#include <functional>
#include "Socket.hpp"

using func_t = std::function<std::string(std::string &package)>;

class TcpServer
{
public:
    TcpServer(uint16_t port)
        : port_(port)
    {
    }
    void Init()
    {
        listensock_.Socket();
        int opt = 1;
        int n = setsockopt(listensock_.GetFd(), SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
        listensock_.Bind(port_);

        listensock_.Listen();
        lg(INFO, "Server Init");
    }

    void Start()
    {
        while (true)
        {

            std::string ip;
            uint16_t port;
            int fd = listensock_.Accept(&ip, &port);

            lg(INFO, "Server Accept");
            sleep(10);
            lg(INFO, "Server close");
            close(fd);
        }
    }

private:
    uint16_t port_;
    Sock listensock_;
};